﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace AspnetUtil.Web.Filter.Auth
{
    /// <summary>
    /// 用户信息 
    /// </summary>
    public class UserInfo : UserInfo<object>
    {
    }

    /// <summary>
    /// 简短的用户信息
    /// </summary>
    public class UserInfo<T>
    {
        public long UserId { get; set; }
        public string UserName { get; set; }

        /// <summary>
        /// 原始用户信息
        /// </summary>
        public T Object { get; set; }
    }

    

    /// <summary>
    /// 当前登录用户的信息
    /// </summary>
    public class UserAttr
    {
        private static readonly ThreadLocal<UserInfo> CacheUser = new();

        /// <summary>
        ///  当前登录用户的信息, 如果未登录, 则返回空
        /// </summary>
        /// <returns></returns>
        public static UserInfo Current()
        {
            return CacheUser.Value;
        }

        /// <summary>
        /// 获取用户 id
        /// </summary>
        /// <returns></returns>
        public static long GetUid()
        {
            var userInfo = Current();
            return userInfo?.UserId ?? 0;
        }

        /// <summary>
        /// 设置当前登录用户的信息
        /// </summary>
        /// <param name="info"></param>
        public static void SetUser(UserInfo info)
        {
            CacheUser.Value = info;
        }
    }
}